﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.AuthorityManageForms.RoleForms
{
    public partial class RoleMainForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.RoleBll roleBll = new BLL.RoleBll();
        public RoleMainForm()
        {
            InitializeComponent();
        }



        private void RoleMainForm_Load(object sender, EventArgs e)
        {
            InitGrid();
        }

        private void InitGrid()
        {
            var models = roleBll.LoadEntitiesAsNoTracking(m => true).OrderByDescending(m => m.RoleID).ToList();
            this.gridControl1.DataSource = models;
        }

        private void ButtonItem_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RoleDetailForm form = new RoleDetailForm(InitGrid);
            form.Show();
        }

        private void ButtonItem_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("RoleID"));
            Role role = roleBll.LoadEntities(m => m.RoleID == id).FirstOrDefault();
            if (role != null)
            {
                RoleDetailForm form = new RoleDetailForm(role, InitGrid);
                form.Show();
            }
        }

        private void ButtonItem_Delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                DialogResult result = XtraMessageBox.Show(ResourceOverall.ConfirmDelete, ResourceOverall.SystemWarningStr, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("RoleID"));
                    int count = db.LoginUsers.Where(m => m.RoleID == id).Count();
                    if (count>0)
                    {
                        XtraMessageBox.Show(ResourceOverall.CannotDelete);
                        return;
                    }
                    Role role = db.Roles.Find(id);
                    if (role != null)
                    {
                        db.Roles.Remove(role);
                        bool res = db.SaveChanges() > 0;
                        if (res)
                        {
                            InitGrid();
                            XtraMessageBox.Show(ResourceOverall.DeleteSucceed);
                        }
                    }
                }
            }
        }

        private void ButtonItem_Seting_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("RoleID"));
            Role role = roleBll.LoadEntities(m => m.RoleID == id).FirstOrDefault();
            if (role != null)
            {
                RoleAuthorityForm form = new RoleAuthorityForm(role);
                form.Show();
            }

        }


    }
}